package net.kong.sns.rest.client.model;

import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;


/**
 * @author yk
 * @version $Id: UserRelation.java,v 1.3 2009/08/04 07:13:46 yongkun Exp $
 */
public class UserRelation extends AbstractJsonAppObject{
    private long fromUserId;
    private long toUserId;
    public static enum RELATION_TYPE {
        FRIEND,
        STROMGER,
        BLACK,
    };
    private RELATION_TYPE relation;
    
    public enum Field {
        user_ids1, user_ids2, type;

        @Override
        public String toString() {
            return name().toLowerCase();
        }
    }

    public UserRelation(){
        
    }
    public UserRelation(long fromUserId, long toUserId, RELATION_TYPE relation) {
        this.fromUserId = fromUserId;
        this.toUserId = toUserId;
        this.relation = relation;
    }
    public long getFromUserId() {
        return fromUserId;
    }
    
    public void setFromUserId(long fromUserId) {
        this.fromUserId = fromUserId;
    }
    
    public long getToUserId() {
        return toUserId;
    }
    
    public void setToUserId(long toUserId) {
        this.toUserId = toUserId;
    }
    
    public RELATION_TYPE getRelation() {
        return relation;
    }
    
    public void setRelation(RELATION_TYPE relation) {
        this.relation = relation;
    }

    @Override
    protected void processJsonObject(Map<String, Object> stringObjectMap) {
        if (stringObjectMap != null) {
            Set<String> fields = getInitializedFields();
            for (String fieldName : fields) {
                Object o = (Object) stringObjectMap.get(fieldName);
                if (o != null) {
                    if (fieldName.equals(Field.user_ids1.toString())) {
                        this.fromUserId = Long.parseLong((String) o);
                    }
                    else if (fieldName.equals(Field.user_ids2.toString())) {
                        this.toUserId = Long.parseLong((String) o);
                    }
                    else if (fieldName.equals(Field.type.toString())) {
                        this.relation = RELATION_TYPE.values()[((Number) o).intValue()-1];
                    }
                }
            }
        }
    }
    /**
     * @see java.lang.Object#equals(Object)
     */
    public boolean equals(Object object) {
        if (!(object instanceof UserRelation)) {
            return false;
        }
        UserRelation rhs = (UserRelation) object;
        return new EqualsBuilder().append(this.toUserId, rhs.toUserId).append(this.relation, rhs.relation).append(this.fromUserId, rhs.fromUserId)
                .isEquals();
    }
    /**
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return new ToStringBuilder(this).append("relation", this.relation).append("toUserId", this.toUserId).append("fromUserId", this.fromUserId)
                .append("initializedFields", this.getInitializedFields()).toString();
    }
}
